package com.yang.aiDoctor.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.aiDoctor.bean.ChatRecord;
import com.yang.aiDoctor.mapper.ChatRecordMapper;
import com.yang.aiDoctor.service.ChatRecordService;
import com.yang.aiDoctor.utils.ChatTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ChatRecordServiceImpl implements ChatRecordService {

    @Value("${spring.ai.openai.chat.options.role}")
    private String aiRole;

    @Resource
    private ChatRecordMapper chatRecordMapper;

    private final OpenAiChatModel chatModel;

    @Autowired
    public ChatRecordServiceImpl(OpenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @Override
    public void saveChatRecord(String userName, String message, ChatTypeEnum chatType) {

        ChatRecord chatRecord = ChatRecord.builder()
                .familyMember(userName)
                .content(message)
                .chatTime(LocalDateTime.now())
                .chatType(chatType.type).build();

        chatRecordMapper.insert(chatRecord);
    }

    @Override
    public List<ChatRecord> getChatRecordList(String userName) {
        QueryWrapper<ChatRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("family_member", userName);
        queryWrapper.orderByAsc("chat_time");
        return chatRecordMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> aiChatStream2(String userName, String message) {
        SystemMessage systemMessage = new SystemMessage(aiRole);
        UserMessage userMessage = new UserMessage(message);
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
        Flux<ChatResponse> responseFlux = this.chatModel.stream(prompt);
        List<String> responses = responseFlux.toStream().map(chatResponse -> {
            String text = chatResponse.getResult().getOutput().getText();
            return text;
        }).toList();
        
        // 保存聊天记录
        saveChatRecord(userName, message, ChatTypeEnum.USER);
        String aiResponse = String.join("", responses);
        saveChatRecord("AI", aiResponse, ChatTypeEnum.BOT);
        
        return responses;
    }

    @Override
    public Flux<ChatResponse> aiChatStream(String userName, String message) {
        SystemMessage systemMessage = new SystemMessage(aiRole);
        UserMessage userMessage = new UserMessage(message);
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
        
        // 保存用户的聊天记录
        saveChatRecord(userName, message, ChatTypeEnum.USER);
        
        return this.chatModel.stream(prompt);
    }


}
